package com.ruoyi.home.service;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.contract.domain.Cproject;
import com.ruoyi.finance.domain.FPayment;
import com.ruoyi.finance.mapper.FinanceMapper;
import com.ruoyi.finance.model.Pfinance;
import com.ruoyi.home.mapper.HomeMapper;
import com.ruoyi.home.model.ProjectStatistics;
import com.ruoyi.project.mapper.HandleMapper;
import com.ruoyi.project.model.BriefProject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class HomeServiceImpl implements IHomeService{

    @Autowired
    private HomeMapper homeMapper;
    @Autowired
    HandleMapper handleMapper;
    @Autowired
    FinanceMapper financeMapper;

    @Override
    public Map<Integer, Map<String,Long>> getStatistics() {
        int currYear = Calendar.getInstance().get(Calendar.YEAR);
        int startYear = currYear - 3;
        Map<Integer, Map<String,Long>> map = new HashMap<>();
        for (int j = startYear; j <= currYear ; j++) {
            map.put(j, new HashMap<>());
        }
        Map<Integer, Map<String, Long>> htMap = getHTStatistics(startYear, currYear);
        for (Map.Entry<Integer, Map<String, Long>> entry : htMap.entrySet()) {
            map.get(entry.getKey()).putAll(entry.getValue());
        }
        Map<Integer, Map<String, Long>> syMap = getSYStatistics(startYear,currYear);
        for (Map.Entry<Integer, Map<String, Long>> entry : syMap.entrySet()) {
            map.get(entry.getKey()).putAll(entry.getValue());
        }
        Map<Integer, Map<String, Long>> tzMap = getTZStatistics(startYear,currYear);
        for (Map.Entry<Integer, Map<String, Long>> entry : tzMap.entrySet()) {
            map.get(entry.getKey()).putAll(entry.getValue());
        }
        Map<Integer, Map<String, Long>> hkMap = getHKStatistics(startYear, currYear);
        for (Map.Entry<Integer, Map<String, Long>> entry : hkMap.entrySet()) {
            map.get(entry.getKey()).putAll(entry.getValue());
        }
        Map<Integer, Map<String, Long>> jtMap = getJTStatistics(startYear, currYear);
        for (Map.Entry<Integer, Map<String, Long>> entry : jtMap.entrySet()) {
            map.get(entry.getKey()).putAll(entry.getValue());
        }
        return map;
    }

    @Override
    public Map<Integer, Map<String,Long>> getHTStatistics(Integer startYear, Integer endYear) {
        int i = handleMapper.hasAdmin(SecurityUtils.getUserId());
        Map<Integer, Map<String,Long>> map = new HashMap<>();
        for (int j = startYear; j <= endYear ; j++) {
            map.put(j, new HashMap<>());
        }
        if (i>0){
            List<ProjectStatistics> ht = homeMapper.selectHTStatistics(1L,"HT", startYear, endYear);
            for (ProjectStatistics e : ht) {
                map.get(e.getYear()).put("HTSL",e.getNum());
                map.get(e.getYear()).put("HTJE",e.getSum());
            }
        } else{
            List<ProjectStatistics> ht = homeMapper.selectHTStatistics(SecurityUtils.getUserId(),"HT", startYear, endYear);
            for (ProjectStatistics e : ht) {
                map.get(e.getYear()).put("HTSL",e.getNum());
                map.get(e.getYear()).put("HTJE",e.getSum());
            }
        }
        return map;
    }

    @Override
    public Map<Integer, Map<String,Long>> getSYStatistics(Integer startYear, Integer endYear) {
        int i = handleMapper.hasAdmin(SecurityUtils.getUserId());
        Map<Integer, Map<String,Long>> map = new HashMap<>();
        for (int j = startYear; j <= endYear ; j++) {
            map.put(j, new HashMap<>());
        }
        if (i>0){
            List<ProjectStatistics> sy = homeMapper.selectHTStatistics(1L, "SY", startYear, endYear);
            for (ProjectStatistics e : sy) {
                map.get(e.getYear()).put("SYSL",e.getNum());
                map.get(e.getYear()).put("SYJE",e.getSum());
            }
        } else{
            List<ProjectStatistics> sy = homeMapper.selectHTStatistics(SecurityUtils.getUserId(), "SY", startYear, endYear);
            for (ProjectStatistics e : sy) {
                map.get(e.getYear()).put("SYSL",e.getNum());
                map.get(e.getYear()).put("SYJE",e.getSum());
            }
        }
        return map;
    }

    @Override
    public Map<Integer, Map<String,Long>> getTZStatistics(Integer startYear, Integer endYear) {
        int i = handleMapper.hasAdmin(SecurityUtils.getUserId());
        Map<Integer, Map<String,Long>> map = new HashMap<>();
        for (int j = startYear; j <= endYear ; j++) {
            map.put(j, new HashMap<>());
        }
        if (i>0){
            List<ProjectStatistics> tz = homeMapper.selectHTStatistics(1L, "TZ", startYear, endYear);
            for (ProjectStatistics e : tz) {
                map.get(e.getYear()).put("TZSL",e.getNum());
                map.get(e.getYear()).put("TZJE",e.getSum());
            }
        }
        else{
            List<ProjectStatistics> tz = homeMapper.selectHTStatistics(SecurityUtils.getUserId(), "TZ", startYear, endYear);
            for (ProjectStatistics e : tz) {
                map.get(e.getYear()).put("TZSL",e.getNum());
                map.get(e.getYear()).put("TZJE",e.getSum());
            }
        }
        return map;
    }

    @Override
    public Map<Integer, Map<String,Long>> getHKStatistics(Integer startYear, Integer endYear) {
        int i = handleMapper.hasAdmin(SecurityUtils.getUserId());
        Map<Integer, Map<String,Long>> map = new HashMap<>();
        for (int j = startYear; j <= endYear ; j++) {
            map.put(j, new HashMap<>());
        }
        if (i>0){
            List<ProjectStatistics> un = homeMapper.selectHKStatistics(1L,0, startYear, endYear);
            List<ProjectStatistics> has = homeMapper.selectHKStatistics(1L,1, startYear, endYear);
            for (ProjectStatistics e : un) {
                map.get(e.getYear()).put("HK0",e.getSum());
            }
            for (ProjectStatistics e : has) {
                map.get(e.getYear()).put("HK1",e.getSum());
            }
        } else{
            List<ProjectStatistics> un = homeMapper.selectHKStatistics(SecurityUtils.getUserId(),0, startYear, endYear);
            List<ProjectStatistics> has = homeMapper.selectHKStatistics(SecurityUtils.getUserId(),1, startYear, endYear);
            for (ProjectStatistics e : un) {
                map.get(e.getYear()).put("HK0",e.getSum());
            }
            for (ProjectStatistics e : has) {
                map.get(e.getYear()).put("HK1",e.getSum());
            }
        }
        return map;
    }

    @Override
    public Map<Integer, Map<String,Long>> getJTStatistics(Integer startYear, Integer endYear) {
        int i = handleMapper.hasAdmin(SecurityUtils.getUserId());
        Map<Integer, Map<String,Long>> map = new HashMap<>();
        for (int j = startYear; j <= endYear ; j++) {
            map.put(j, new HashMap<>());
        }
        if (i>0){
            List<ProjectStatistics> all = homeMapper.selectJTStatistics(1L,0, startYear, endYear);
            List<ProjectStatistics> has = homeMapper.selectJTStatistics(1L,5, startYear, endYear);
            for (ProjectStatistics e : all) {
                map.get(e.getYear()).put("JT0",e.getNum());
            }
            for (ProjectStatistics e : has) {
                map.get(e.getYear()).put("JT1",e.getNum());
                Map<String, Long> t = map.get(e.getYear());
                t.put("JT0",t.get("JT0")-e.getNum());
            }
        } else{
            List<ProjectStatistics> all = homeMapper.selectJTStatistics(SecurityUtils.getUserId(),0, startYear, endYear);
            List<ProjectStatistics> has = homeMapper.selectJTStatistics(SecurityUtils.getUserId(),5, startYear, endYear);
            for (ProjectStatistics e : all) {
                map.get(e.getYear()).put("JT0",e.getNum());
            }
            for (ProjectStatistics e : has) {
                map.get(e.getYear()).put("JT1",e.getNum());
                Map<String, Long> t = map.get(e.getYear());
                t.put("JT0",t.get("JT0")-e.getNum());
            }
        }
        return map;
    }

    @Override
    public List<Cproject> getHtList(Integer year, String type) {

        int i = handleMapper.hasAdmin(SecurityUtils.getUserId());
        PageUtils.startPage();
        if(i>0){
            return homeMapper.selectHtList(1L,year,type);
        }else {
            return homeMapper.selectHtList(SecurityUtils.getUserId(), year, type);
        }
    }

    @Override
    public List<Pfinance> getCwList(Integer year) {
        int i = handleMapper.hasAdmin(SecurityUtils.getUserId());
        List<Pfinance> pfinances=null;
        PageUtils.startPage();
        if(i>0){
            pfinances= homeMapper.selectCwProject(1L,year);
        }else {
            pfinances= homeMapper.selectCwProject(SecurityUtils.getUserId(),year);
        }
        for (Pfinance pfinance : pfinances) {
            List<FPayment> fPayments = financeMapper.selectPaymentByPid(pfinance.getProjectId());
            long hasPay=0;
            long sum=0;
            int count=0;
            int n=1;
            for (FPayment fPayment : fPayments) {
                fPayment.setNum(n++);
                sum+=fPayment.getPaymentPrice();
                if (fPayment.getAccountPrice()!=null){
                    hasPay+=fPayment.getAccountPrice();
                    count++;
                }
            }
            pfinance.setPay(fPayments);
            pfinance.setHasPay(hasPay);
            pfinance.setUnPay(sum-hasPay);
            pfinance.setHasPayCount(count);

            boolean flag = getType(pfinance.getProjectId());
            pfinance.setType(flag);
        }
        return pfinances;
    }

    @Override
    public List<BriefProject> getJtList(Boolean finish,Integer year) {
        int i = handleMapper.hasAdmin(SecurityUtils.getUserId());
        List<BriefProject> res=null;
        PageUtils.startPage();
        if(i>0){
            res= homeMapper.selectJtProject(1L,finish, year);
        }else {
            res= homeMapper.selectJtProject(SecurityUtils.getUserId(),finish, year);
        }
        return res;
    }

    public boolean getType(Long pid){
        if(financeMapper.getProjectStyle(pid)!=5){
            return false;
        }
        if(financeMapper.countPaymentByPid(pid)!=0){
            return false;
        }
        if(financeMapper.countExpByProjectId4(pid)!=0){
            return false;
        }
        return true;
    }
}
